package middleware

import (
	"fitness/pkg/constant"
	"fitness/pkg/tools"
	"github.com/gin-gonic/gin"

	"net/http"
	"strings"
)

// 小程序获取用户信息
func MpJwt(c *gin.Context) {
	token := c.GetHeader(constant.HeaderSignToken)
	if token == "" {
		token = c.Query(constant.HeaderSignToken)
	}
	if token != "" {
		token = strings.ReplaceAll(token, constant.HeaderSignTokenStr, "")
	}
	fitnessUserInfo := tools.ParseToken(token)
	if fitnessUserInfo == nil || fitnessUserInfo[constant.ID] == nil || fitnessUserInfo[constant.OPEN_ID] == nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "您尚未登录，请登录并绑定手机号",
		})
		c.Abort()
		return
	}

	c.Set(constant.ID, uint64(fitnessUserInfo[constant.ID].(float64)))
	c.Set(constant.OPEN_ID, fitnessUserInfo[constant.OPEN_ID])
}
